const StringAlgorithms = require("../template")

function testAdvancedAlgorithms() {
  const stringAlgo = new StringAlgorithms()

  console.log(
    "1. 最长公共子序列测试:",
    (() => {
      const tests = [
        { str1: "abcde", str2: "ace", expected: 3 },
        { str1: "abc", str2: "abc", expected: 3 },
        { str1: "abc", str2: "def", expected: 0 },
        { str1: "", str2: "abc", expected: 0 },
        { str1: "abc", str2: "", expected: 0 },
        { str1: "", str2: "", expected: 0 }
      ]
      return tests.every(test => stringAlgo.longestCommonSubsequence(test.str1, test.str2) === test.expected)
    })()
  )

  console.log(
    "2. 最长公共子串测试:",
    (() => {
      const tests = [
        { str1: "abcde", str2: "ace", expected: 1 },
        { str1: "abc", str2: "abc", expected: 3 },
        { str1: "abc", str2: "def", expected: 0 },
        { str1: "", str2: "abc", expected: 0 },
        { str1: "abc", str2: "", expected: 0 },
        { str1: "", str2: "", expected: 0 }
      ]
      return tests.every(test => stringAlgo.longestCommonSubstring(test.str1, test.str2) === test.expected)
    })()
  )

  console.log(
    "3. 编辑距离测试:",
    (() => {
      const tests = [
        { str1: "kitten", str2: "sitting", expected: 3 },
        { str1: "abc", str2: "abc", expected: 0 },
        { str1: "abc", str2: "def", expected: 3 },
        { str1: "", str2: "abc", expected: 3 },
        { str1: "abc", str2: "", expected: 3 },
        { str1: "", str2: "", expected: 0 }
      ]
      return tests.every(test => stringAlgo.editDistance(test.str1, test.str2) === test.expected)
    })()
  )

  console.log(
    "4. 字符串哈希测试:",
    (() => {
      const tests = [
        { input: "hello", expected: true },
        { input: "world", expected: true },
        { input: "a", expected: true },
        { input: "", expected: true },
        { input: "123", expected: true }
      ]
      return tests.every(test => {
        const hash1 = stringAlgo.stringHash(test.input)
        const hash2 = stringAlgo.stringHash(test.input)
        return hash1 === hash2 && typeof hash1 === 'number'
      })
    })()
  )

  console.log(
    "5. 字符串相似度测试:",
    (() => {
      const tests = [
        { str1: "hello", str2: "hello", expected: 1.0 },
        { str1: "hello", str2: "world", expected: 0.0 },
        { str1: "abc", str2: "ab", expected: 0.67 },
        { str1: "", str2: "", expected: 1.0 },
        { str1: "a", str2: "b", expected: 0.0 }
      ]
      return tests.every(test => {
        const similarity = stringAlgo.stringSimilarity(test.str1, test.str2)
        return Math.abs(similarity - test.expected) < 0.1
      })
    })()
  )

  console.log(
    "6. 字符串编码测试:",
    (() => {
      const tests = [
        { input: "hello", expected: true },
        { input: "world", expected: true },
        { input: "a", expected: true },
        { input: "", expected: true },
        { input: "123", expected: true }
      ]
      return tests.every(test => {
        const encoded = stringAlgo.encodeString(test.input)
        const decoded = stringAlgo.decodeString(encoded)
        return decoded === test.input
      })
    })()
  )

  console.log(
    "7. 字符串压缩算法测试:",
    (() => {
      const tests = [
        { input: "aabcccccaaa", expected: "a2b1c5a3" },
        { input: "abc", expected: "a1b1c1" },
        { input: "a", expected: "a1" },
        { input: "", expected: "" },
        { input: "aabbcc", expected: "a2b2c2" }
      ]
      return tests.every(test => {
        const compressed = stringAlgo.advancedCompress(test.input)
        const decompressed = stringAlgo.advancedDecompress(compressed)
        return decompressed === test.input
      })
    })()
  )

  console.log(
    "8. 字符串模式匹配测试:",
    (() => {
      const tests = [
        { input: "hello", pattern: "h*llo", expected: true },
        { input: "hello", pattern: "h?llo", expected: true },
        { input: "hello", pattern: "h*", expected: true },
        { input: "hello", pattern: "h?*", expected: true },
        { input: "hello", pattern: "world", expected: false }
      ]
      return tests.every(test => stringAlgo.patternMatch(test.input, test.pattern) === test.expected)
    })()
  )

  console.log(
    "9. this上下文高级算法测试:",
    (() => {
      const advancedObj = {
        multiplier: 2,
        processAdvanced: function(str1, str2) {
          const stringAlgo = new StringAlgorithms()
          const distance = stringAlgo.editDistance(str1, str2)
          return distance * this.multiplier
        }
      }
      return advancedObj.processAdvanced("abc", "def") === 6 // 3 * 2 = 6
    })()
  )

  console.log(
    "10. 复杂对象this绑定高级算法测试:",
    (() => {
      const advancedConfigObj = {
        config: { threshold: 2 },
        processWithConfig: function(str1, str2) {
          const stringAlgo = new StringAlgorithms()
          const distance = stringAlgo.editDistance(str1, str2)
          return distance <= this.config.threshold
        }
      }
      return advancedConfigObj.processWithConfig("abc", "ab") === true &&
             advancedConfigObj.processWithConfig("abc", "def") === false
    })()
  )

  console.log(
    "11. 高级算法一致性测试:",
    (() => {
      const str1 = "abcde"
      const str2 = "ace"
      const lcs = stringAlgo.longestCommonSubsequence(str1, str2)
      const lcsubstr = stringAlgo.longestCommonSubstring(str1, str2)
      const distance = stringAlgo.editDistance(str1, str2)
      const similarity = stringAlgo.stringSimilarity(str1, str2)
      
      return lcs === 3 &&
             lcsubstr === 1 &&
             distance > 0 &&
             similarity >= 0 && similarity <= 1
    })()
  )

  console.log(
    "12. 高级算法边界测试:",
    (() => {
      const emptyResult = stringAlgo.longestCommonSubsequence("", "") === 0 &&
                         stringAlgo.longestCommonSubstring("", "") === 0 &&
                         stringAlgo.editDistance("", "") === 0 &&
                         stringAlgo.stringSimilarity("", "") === 1.0
      
      const singleCharResult = stringAlgo.longestCommonSubsequence("a", "a") === 1 &&
                              stringAlgo.longestCommonSubstring("a", "a") === 1 &&
                              stringAlgo.editDistance("a", "a") === 0 &&
                              stringAlgo.stringSimilarity("a", "a") === 1.0
      
      return emptyResult && singleCharResult
    })()
  )

  console.log(
    "13. 高级算法性能测试:",
    (() => {
      const str1 = "a".repeat(100) + "b".repeat(100)
      const str2 = "a".repeat(100) + "c".repeat(100)
      const start = performance.now()
      stringAlgo.longestCommonSubsequence(str1, str2)
      stringAlgo.longestCommonSubstring(str1, str2)
      stringAlgo.editDistance(str1, str2)
      stringAlgo.stringSimilarity(str1, str2)
      const end = performance.now()
      return (end - start) < 1000 // 性能测试应该在1秒内完成
    })()
  )

  console.log(
    "14. 高级算法复杂测试:",
    (() => {
      const complexStr1 = "Hello World! 123"
      const complexStr2 = "Hello Universe! 456"
      const lcs = stringAlgo.longestCommonSubsequence(complexStr1, complexStr2)
      const lcsubstr = stringAlgo.longestCommonSubstring(complexStr1, complexStr2)
      const distance = stringAlgo.editDistance(complexStr1, complexStr2)
      const similarity = stringAlgo.stringSimilarity(complexStr1, complexStr2)
      const encoded = stringAlgo.encodeString(complexStr1)
      const decoded = stringAlgo.decodeString(encoded)
      
      return lcs > 0 &&
             lcsubstr > 0 &&
             distance > 0 &&
             similarity > 0 && similarity < 1 &&
             decoded === complexStr1
    })()
  )
}

testAdvancedAlgorithms()
